from goods.models import GoodsChannel
def get_category():
    # 先定义一个字典将商品类别的数据存入
    categories = {}  # {'1':'channels:[],sub_cats:[]',}
    '''
     categories['a']= b
     categories['a']= c
    '''
    # 先来获取所有一级分类的分组数据
    channels_qs = GoodsChannel.objects.all().order_by('group_id', 'sequence')
    for channel in channels_qs:
        group_id = channel.group_id
        # 先生成频道组
        if group_id not in categories:
            categories[group_id] = {"channels": [], "sub_cats": []}
        # 通过频道查询到一级分类
        cat1 = channel.category
        categories[group_id]['channels'].append(cat1)
        # 通过一级分类查询二级分类
        # category1.goodscategory_set.all()
        cat2_qs = cat1.subs.all()
        for cat2 in cat2_qs:
            # 通过耳机分类再查询所有三级分类
            cat3_qs = cat2.subs.all()
            # 给每一个二级分类额外添加一个属性sub_category，将其下属的所有三级分类赋值给他
            cat2.sub_cats = cat3_qs
            categories[group_id]['sub_cats'].append(cat2)

    return categories


def get_breadcrumb(category):
    '''传入的参数是三级分类，通过三级分类去找到对应的二级和一级分类'''
    breadcrumb={
        'cat1':category.parent.parent,
        'cat2':category.parent,
        'cat3':category,
    }
    return breadcrumb